package com.oa.modules.approvalsec1.controller;

import com.oa.common.utils.FileUtil;
import com.oa.common.utils.R;
import com.oa.common.utils.SenInfoCheckUtil;
import com.oa.modules.approvalsec1.entity.CompanyInfoEntity;
import com.oa.modules.approvalsec1.service.CompanyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "设置进度")
@RequestMapping("setProgress")
public class SetProgressController {
    @Autowired
    private CompanyInfoService companyInfoService;

    @PostMapping("/progress")
    @ApiOperation(value = "设置进度",httpMethod = "POST")
    @ApiImplicitParam(name = "file,name", value = "传参:file,companyInfo:公司信息,companyType:企业类型,", required = true)
    public R progress(@RequestParam("file") MultipartFile file, CompanyInfoEntity companyInfoEntity) throws IllegalStateException, IOException {
        if (file != null) {
            String fileName = file.getOriginalFilename();// 文件原名称
            String accessToken = SenInfoCheckUtil.getAccessToken();
            if(SenInfoCheckUtil.imgFilter(accessToken,file)){
                String imageUrl = FileUtil.ImageUpload2(file);
                File newFile = new File(imageUrl);
                if (!newFile.isFile()) {
                    return R.error(1,imageUrl);
                }
//                String imageHtmlUrl = imageUrl.replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
                String imageHtmlUrl = imageUrl.replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
                Map<String,Object> urlMap = new HashMap<>();
                urlMap.put("imageUrl",imageUrl);
                urlMap.put("imageHtmlUrl",imageHtmlUrl);
                companyInfoEntity.setLicenseUrl(imageHtmlUrl);
                companyInfoEntity.setStatus("0");
                companyInfoService.save(companyInfoEntity);
                return R.ok().put("urlMap",urlMap);
            }else{
                return R.error(1,"文件内容涉及敏感内容，请重新上传。");
            }
        }else{
            return R.error(1,"文件为空");
        }
    }
    @PostMapping("/dataCheck")
    @ApiOperation(value = "审核",httpMethod = "POST")
    @ApiImplicitParam(name = "file,name", value = "传参:id,status:1审核通过，2驳回,rejectReason:驳回原因", required = true)
    public R dataCheck(@RequestBody CompanyInfoEntity companyInfoEntity) {
        boolean flag = companyInfoService.updateById(companyInfoEntity);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
